package com.sgb.kjwl.viewmodel.enterprise;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.enterprise.CopyrightListEntity2;
import com.sgb.kjwl.model.entity.enterprise.IntellpropEntity;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.PatentEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.fragment.enterprise.IntellpropFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class IntellpropFragModel extends BaseViewModel {
    private IntellpropFragment mFragment;
    private int pageNum = 1;
    private int pageNumCr = 1;//版权页数
    IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "", "");


    public String websiteNo;
    public String websiteType;

    public IntellpropFragModel(IntellpropFragment intellpropFragment) {
        this.mFragment = intellpropFragment;
    }


    public void getData(int type, Map map) {
        switch (type) {
            case 0: // 商标
                trademarkList(false, map);
                break;
            case 1: //版权
                getCopyrightList(false, map);
                break;
            case 2: //专利
                getPatentList(false, map);
                break;
        }
    }


    public void getCopyrightList(boolean isLoadMore, Map map) {
        if (!isLoadMore){
            mFragment.loading();
        }
        pageNumCr = isLoadMore ? pageNumCr + 1 : 1;

        if (!TextUtils.isEmpty(websiteType)&&!TextUtils.isEmpty(websiteNo)) {
            switch (Integer.parseInt(websiteType)) {
                case WebsiteInfoActivity.WEBSITE_FOR_PERSONAL:
                    map.put("userNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_TEAM:
                    map.put("teamNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_COMPANY:
                    map.put("enterpriseNo",websiteNo);
                    break;
            }
        }
        //pageSize
        map.put("limit", 20);
        //pageNum
        map.put("page", pageNumCr);
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getCopyrightQueryList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CopyrightListEntity2>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CopyrightListEntity2>>>> call, Response<BaseEntity<ListResponse<List<CopyrightListEntity2>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showCopyrightList(response.body().getData().getList(), isLoadMore);
                    mFragment.success();
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                    mFragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CopyrightListEntity2>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                mFragment.error();
            }
        });

    }

    /**
     * 获取商标类型
     */
    public void getTrademarkTypeOption() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTrademarkTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                    list1.addAll(response.body().getData());
                    list1.add(0, entity1);
                    mFragment.showBrandPopWindow(list1);
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取价格选项
     */
    public void getPriceOption(int position) {
        switch (position) {
            case 0:
                EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTrademarkPriceOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                        if (response.body() == null || response.body().getData() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                            list1.addAll(response.body().getData());
                            list1.add(0, entity1);
                            mFragment.showPricePopWindow1(list1);
                        } else {
                            MToast.showToast(mFragment.getActivity(), "暂无数据");
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
                break;
            case 1:
                EnterpriseAPIManager.getInstance().getEnterpriseAPI().getCopyrightPriceOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                        if (response.body() == null || response.body().getData() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            ArrayList<IpPickSelectEntity> list2 = new ArrayList<>();
                            list2.addAll(response.body().getData());
                            list2.add(0, entity1);
                            mFragment.showPricePopWindow2(list2);
                        } else {
                            MToast.showToast(mFragment.getActivity(), "暂无数据");
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
                break;
            case 2:
                EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPatentPriceOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                        if (response.body() == null || response.body().getData() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            ArrayList<IpPickSelectEntity> list3 = new ArrayList<>();
                            list3.addAll(response.body().getData());
                            list3.add(0, entity1);
                            mFragment.showPricePopWindow3(list3);
                        } else {
                            MToast.showToast(mFragment.getActivity(), "暂无数据");
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
                break;
        }
    }


    /**
     * 获取组合类型
     */
    public void getTypeOption() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                    list1.addAll(response.body().getData());
                    list1.add(0, entity1);
                    mFragment.showMakeupPopWindow(list1);
                } else {
                    MToast.showToast(mFragment.getActivity(), "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取长度选项
     */
    public void getStrLengthOption() {
        NewAPIManager.getInstance().getEnterPrisenewAPI().getStrLengthOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData().size() != 0) {
                    ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                    list1.addAll(response.body().getData());
                    list1.add(0, entity1);
                    mFragment.showLengthPopWindow(list1);
                } else {
                    MToast.showToast(mFragment.getActivity(), "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /*
     * 获取版权分类选项
     */
    public void getCopyrightTypeOption() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getCopyrightTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(mFragment.getActivity(), "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                    list1.addAll(response.body().getData());
                    list1.add(0, entity1);
                    mFragment.showCopyrightPopWindow(list1);
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * 商标列表查询
     */
    public void trademarkList(boolean isLodMore, Map map) {
        if (!isLodMore){
            mFragment.loading();
        }
        pageNum = isLodMore ? pageNum + 1 : 1;
        if (!TextUtils.isEmpty(websiteType)&&!TextUtils.isEmpty(websiteNo)) {
            switch (Integer.parseInt(websiteType)) {
                case WebsiteInfoActivity.WEBSITE_FOR_PERSONAL:
                    map.put("userNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_TEAM:
                    map.put("teamNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_COMPANY:
                    map.put("enterpriseNo",websiteNo);
                    break;
            }
        }
        //pageSize
        map.put("limit", 20);
        //pageNum
        map.put("page", pageNum);
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().trademarkLists(map).enqueue(new Callback<BaseEntity<ListResponse<List<IntellpropEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<IntellpropEntity>>>> call, Response<BaseEntity<ListResponse<List<IntellpropEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showList(response.body().getData().getList(), isLodMore);
                    mFragment.success();
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                    mFragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<IntellpropEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                mFragment.error();
            }
        });
    }

    /*
     * 获取是否独家选项 本地写入数据
     */
    public void getIsOnlyOption() {
        List<IpPickSelectEntity> onlyDatas = new ArrayList<>();
        IpPickSelectEntity entity = new IpPickSelectEntity();
        entity.setName("不限");
        entity.setPublishType("");
        onlyDatas.add(entity);

        entity = new IpPickSelectEntity();
        entity.setName("团队");
        entity.setPublishType("2");
        onlyDatas.add(entity);

        entity = new IpPickSelectEntity();
        entity.setName("个人");
        entity.setPublishType("0");
        onlyDatas.add(entity);

        entity = new IpPickSelectEntity();
        entity.setName("企业");
        entity.setPublishType("1");
        onlyDatas.add(entity);
//        IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "", "");
//        IpPickSelectEntity entity2 = new IpPickSelectEntity("1", "独家", "", "", "", "", "");
//        IpPickSelectEntity entity3 = new IpPickSelectEntity("2", "非独家", "", "", "", "", "");

//        onlyDatas.add(entity1);
//        onlyDatas.add(entity2);
//        onlyDatas.add(entity3);
        mFragment.showOnlyPopWindow(onlyDatas);

    }

    /**
     * 专利列表查询
     */
    public void getPatentList(boolean isLodMore, Map map) {
        if (!isLodMore){
            mFragment.loading();
        }
        pageNum = isLodMore ? pageNum + 1 : 1;
        if (!TextUtils.isEmpty(websiteType)&&!TextUtils.isEmpty(websiteNo)) {
            switch (Integer.parseInt(websiteType)) {
                case WebsiteInfoActivity.WEBSITE_FOR_PERSONAL:
                    map.put("userNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_TEAM:
                    map.put("teamNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_COMPANY:
                    map.put("enterpriseNo",websiteNo);
                    break;
            }
        }
        //pageSize
        map.put("limit", 20);
        //pageNum
        map.put("page", pageNum);
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPatentList(map).enqueue(new Callback<BaseEntity<ListResponse<List<PatentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PatentEntity>>>> call, Response<BaseEntity<ListResponse<List<PatentEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showList2(response.body().getData().getList(), isLodMore);
                    mFragment.success();
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                    mFragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PatentEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                mFragment.error();
            }
        });
    }

    /**
     * 专利类型
     */
    public void getPatentTypeOption() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPatentTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<IpPickSelectEntity> list = new ArrayList<>();
                    list.addAll(response.body().getData());
                    list.add(0, entity1);
                    mFragment.showPatentTypePopWindow(list);
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /*
     * 专利状态
     */
    public void getPatentStatus() {
        IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "", "");
        IpPickSelectEntity entity2 = new IpPickSelectEntity("1", "授权未下证", "", "", "", "", "");
        IpPickSelectEntity entity3 = new IpPickSelectEntity("2", "授权已下证", "", "", "", "", "");
        List<IpPickSelectEntity> patentDatas = new ArrayList<>();
        patentDatas.add(entity1);
        patentDatas.add(entity2);
        patentDatas.add(entity3);
        mFragment.showPatentStatePopWindow(patentDatas);

    }

    public void queryTerritoryType() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTerritoryTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<IpPickSelectEntity> list = new ArrayList<>();
                    list.addAll(response.body().getData());
                    list.add(0, entity1);
                    mFragment.showTradePopWindow(list);
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getPriceOptionByNo(int priceServiceNo) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPriceOption(priceServiceNo).enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                    list1.addAll(response.body().getData());
                    list1.add(0, entity1);
                    switch (priceServiceNo) {
                        case 4://商标
                            mFragment.showPricePopWindow1(list1);
                            break;
                        case 5://专利
                            mFragment.showPricePopWindow3(list1);
                            break;
                        case 6://版权
                            mFragment.showPricePopWindow2(list1);
                            break;
                    }

                } else {
                    MToast.showToast(mFragment.getActivity(), "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

}
